package com.synergy.stockflow.sales.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.entity.SysUser;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.Material;
import com.synergy.stockflow.sales.entity.OrderReturnDetails;
import com.synergy.stockflow.sales.entity.OrderReturns;
import com.synergy.stockflow.sales.entity.Quotation;
import com.synergy.stockflow.sales.entity.ShipmentHistory;
import com.synergy.stockflow.sales.service.OrderReturnDetailsService;
import com.synergy.stockflow.sales.service.OrderReturnsService;
import com.synergy.stockflow.sales.service.QuoteDetailsService;
import com.synergy.stockflow.sales.service.ShipmentHistoryService;
import com.synergy.stockflow.service.SysUserService;
import com.synergy.stockflow.warehouse.entity.InventoryTransaction;
import com.synergy.stockflow.warehouse.service.InventoryTransactionService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
    @RequestMapping("/orderRreturns")
public class OrderReturnsController {

    @Autowired
    OrderReturnsService orderReturnsService;
    @Autowired
    ShipmentHistoryService shipmentHistoryService;
    @Autowired
    OrderReturnDetailsService orderReturnDetailsService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    QuoteDetailsService quoteDetailsService;
    @Autowired
    InventoryTransactionService inventoryTransactionService;

    @GetMapping
    public Result<Page<OrderReturns>> getAllOrderReturns(
            @RequestParam(name = "orderReturnsId", required = false) String orderReturnsId,
            @RequestParam(name = "orderReturnsQuotationId", required = false) String orderReturnsQuotationId,
            @RequestParam(name = "orderReturnsWarehouse", required = false) String orderReturnsWarehouse
    ){
        Page<OrderReturns> page = new Page<>(1, 10000);
        page=orderReturnsService.select(page,orderReturnsId,orderReturnsQuotationId,orderReturnsWarehouse);
        for(OrderReturns o:page.getRecords()){
            LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
            o.setNickname(sysUserService.getOne(queryWrapper.eq(SysUser::getId,o.getOrderReturnsAttn())).getNickname());
        }
        return Result.success(page);
    }

    @PostMapping("{isYes}")
    public Result insert(@RequestBody OrderReturns orderReturns, @PathVariable("isYes")String isYes){
        ShipmentHistory s = shipmentHistoryService.getById(orderReturns.getOrderReturnsShipmentHistoryId());
        orderReturns.setOrderReturnsQuotationId(s.getShipmentHistoryQuotationId());
        if(orderReturns.getOrderReturnsTotalQuantity().doubleValue()!=0.00){
            if(isYes.equals("false")){
                orderReturns.setOrderReturnsState("申请中");
            }else{
                orderReturns.setOrderReturnsState("进行中");
            }
            orderReturnsService.save(orderReturns);
            List<OrderReturns> list = orderReturnsService.list();
            for(OrderReturnDetails o:orderReturns.getOrderReturnDetailsList()){
                if(o.getOrderReturnDetailOutboundReturns().doubleValue()!=0.00 || o.getOrderReturnDetailNoOutboundReturns().doubleValue()!=0.00){
                    o.setOrderReturnDetailOrderReturnsId(list.get(list.size()-1).getOrderReturnsId());
                    o.setOrderReturnDetailId("ORD-"+String.valueOf(new Date().getTime()));
                    orderReturnDetailsService.save(o);
                    String sourceDocId=list.get(list.size()-1).getOrderReturnsId();
                    String warehouseId = quoteDetailsService.selectWa(o.getOrderReturnDetailQuoteDetailsId()).getQuoteDetailsWarehouse();
                    double v= o.getOrderReturnDetailOutboundReturns().doubleValue();
                    InventoryTransaction inventoryTransaction = new InventoryTransaction(null,"PO-IN",sourceDocId,"order_returns",warehouseId,o.getOrderReturnDetailProductsId(),o.getOrderReturnDetailId(),new BigDecimal(v),new Date(),"1","销售退回",null,null);
                    inventoryTransactionService.save(inventoryTransaction);
                }
            }
        }

        return Result.success(orderReturns);
    }

    @PutMapping("{isYes}")
    public Result update(@RequestBody OrderReturns orderReturns,@PathVariable("isYes")String isYes){
        if(isYes.equals("false")){
            orderReturns.setOrderReturnsState("申请中");
        }else {
            orderReturns.setOrderReturnsState("进行中");
        }
        orderReturnsService.updateById(orderReturns);
        return Result.success(orderReturns);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody List<OrderReturns> orderReturns){
        for(OrderReturns o:orderReturns){
            orderReturnsService.removeById(o.getOrderReturnsId());
        }
        return Result.success(orderReturns);
    }
}
